package dws_config

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"server/apps/dws/dws_api"
	"server/apps/dws/dws_fld"
	"server/apps/dws/dws_tb"
	"server/global"
	"server/model/common/response"
	"server/utils"
)

type DwsConfigApi struct {
}

var dwsApiService = dws_api.DwsApiService{}
var dwsTbService = dws_tb.DwsTbService{}
var dwsFldService = dws_fld.DwsFldService{}


func (dwsConfigApi *DwsConfigApi) CreateDwsConfig(c *gin.Context) {
	var dwsConfig DwsConfig
	err := c.ShouldBindJSON(&dwsConfig)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	dwsConfig.DwsApi.CreatedBy = utils.GetUserID(c)
	if err := dwsApiService.CreateDwsApi(&dwsConfig.DwsApi); err != nil {
		global.Logger.Error("创建dwsApi失败!", zap.Error(err))
		response.FailWithMessage("创建dwsApi失败", c)
		return
	}
	for idx, oneDwsTb := range dwsConfig.DwsTbs {
		oneDwsTb.CreatedBy = utils.GetUserID(c)
		oneDwsTb.ApiId = dwsConfig.DwsApi.ID
		oneDwsTb.Order = idx

		if idx > 0 {
			if oneDwsTb.JoinType == "" || oneDwsTb.JoinOn == "" {
				global.Logger.Error("联表关系缺失,请修改!")
				response.FailWithMessage("联表关系缺失,请修改!", c)
				return
			}
		}
		if err := dwsTbService.CreateDwsTb(&oneDwsTb); err != nil {
			global.Logger.Error("创建dwsTd失败!", zap.Error(err))
			response.FailWithMessage("创建dwsTd失败", c)
			return
		}
	}
	for _, oneDwsFld := range dwsConfig.DwsFlds {
		oneDwsFld.CreatedBy = utils.GetUserID(c)
		oneDwsFld.ApiId = dwsConfig.DwsApi.ID
		if err := dwsFldService.CreateDwsFld(&oneDwsFld); err != nil {
			global.Logger.Error("创建dwsFld失败!", zap.Error(err))
			response.FailWithMessage("创建dwsFld失败", c)
			return
		}
	}
	response.OkWithMessage("创建dwsApi成功", c)
}


func (dwsConfigApi *DwsConfigApi) DeleteDwsConfig(c *gin.Context) {
	var dwsApi dws_api.DwsApi
	err := c.ShouldBindJSON(&dwsApi)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	dwsApi.DeletedBy = utils.GetUserID(c)
	if err := dwsApiService.DeleteDwsApi(dwsApi); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}

	if err := dwsTbService.DeleteDwsTbByCol("apiId", dwsApi.ID, dwsApi.DeletedBy); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}
	if err := dwsFldService.DeleteDwsFldByCol("apiId", dwsApi.ID, dwsApi.DeletedBy); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}
}


func (dwsConfigApi *DwsConfigApi) UpdateDwsConfig(c *gin.Context) {
	var dwsConfig DwsConfig
	err := c.ShouldBindJSON(&dwsConfig)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	dwsConfig.DwsApi.UpdatedBy = utils.GetUserID(c)
	dwsConfig.DwsApi.DeletedBy = utils.GetUserID(c)
	if err := dwsApiService.UpdateDwsApi(dwsConfig.DwsApi); err != nil {
		global.Logger.Error("更新dwsApi失败!", zap.Error(err))
		response.FailWithMessage("更新dwsApi失败", c)
	}
	if err := dwsTbService.DeleteDwsTbByCol("apiId", dwsConfig.DwsApi.ID, dwsConfig.DwsApi.DeletedBy); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}
	if err := dwsFldService.DeleteDwsFldByCol("apiId", dwsConfig.DwsApi.ID, dwsConfig.DwsApi.DeletedBy); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}

	for idx, oneDwsTb := range dwsConfig.DwsTbs {
		oneDwsTb.CreatedBy = utils.GetUserID(c)
		oneDwsTb.ApiId = dwsConfig.DwsApi.ID
		oneDwsTb.Order = idx

		if idx > 0 {
			if oneDwsTb.JoinType == "" || oneDwsTb.JoinOn == "" {
				global.Logger.Error("联表关系缺失,请修改!")
				response.FailWithMessage("联表关系缺失,请修改!", c)
				return
			}
		}
		if err := dwsTbService.CreateDwsTb(&oneDwsTb); err != nil {
			global.Logger.Error("创建dwsTd失败!", zap.Error(err))
			response.FailWithMessage("创建dwsTd失败", c)
			return
		}
	}
	for _, oneDwsFld := range dwsConfig.DwsFlds {
		oneDwsFld.CreatedBy = utils.GetUserID(c)
		oneDwsFld.ApiId = dwsConfig.DwsApi.ID
		if err := dwsFldService.CreateDwsFld(&oneDwsFld); err != nil {
			global.Logger.Error("创建dwsFld失败!", zap.Error(err))
			response.FailWithMessage("创建dwsFld失败", c)
			return
		}
	}

	response.OkWithMessage("更新dwsConfig成功", c)
}


func (dwsConfigApi *DwsConfigApi) FindDwsConfig(c *gin.Context) {
	var dwsConfig DwsConfig
	var dwsApi dws_api.DwsApi
	err := c.ShouldBindQuery(&dwsApi)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if dwsConfig.DwsApi, err = dwsApiService.GetDwsApiById(dwsApi.ID); err != nil {
		global.Logger.Error("查询dwsApi失败!", zap.Error(err))
		response.FailWithMessage("查询dwsApi失败", c)
		return
	}
	if dwsConfig.DwsTbs, err = dwsTbService.GetDwsTbByCol("apiId", dwsApi.ID); err != nil {
		global.Logger.Error("查询dwsTbs失败!", zap.Error(err))
		response.FailWithMessage("查询dwsTbs失败", c)
		return
	}
	if dwsConfig.DwsFlds, err = dwsFldService.GetDwsFldByCol("apiId", dwsApi.ID); err != nil {
		global.Logger.Error("查询dwsFlds失败!", zap.Error(err))
		response.FailWithMessage("查询dwsFlds失败", c)
		return
	}
	response.OkWithDetailed(gin.H{"dwsConfig": dwsConfig}, "获取成功", c)
}

